<template>
    <mescroll-uni
        ref="mescrollRef"
        @init="mescrollInit"
        :height="height"
        :down="downOption"
        :up="upOption"
        @up="getData"
        @emptyclick="emptyClick">
        <quick-product-list
            ref="proList"
            :tempType="3"
            :productList="datas"
            :displayData="displayData"
            @sku="sku"
            @upFavorite="upFavorite"
            :isLimit="isLimit"></quick-product-list>
        <view class="mark"></view>
    </mescroll-uni>
</template>
<script>
import quickProductList from "@/components/product/quick-product-list.vue"
import { getWholeSaleListForSku } from "@/service/api/product.js"
import utilsOld from "@/utils/utilsOld.js"
import storage from "@/uni-sub-base/utils/storage.js"
export default {
    components: {
        quickProductList,
    },
    props: {
        height: {
            type: String,
            default: "",
        },
        displayData: {
            type: Object,
            default: () => {},
        },
        activity: {
            type: Object,
            default: () => {},
        },
        isLimit: {
            type: Boolean,
            default: false,
        },
    },
    data() {
        return {
            datas: [],
            data: [],
            productList: [],
            param: {
                OrderBy: "AddTime",
                ProductType: 1,
                Sort: "desc",
                minPrice: 0,
                maxPrice: 0,
            },
            priceDecimalPlaces: 2,
        }
    },
    mounted() {
        this.refresh()
    },
    methods: {
        getData(page) {
            let param = this.param
            param.infoType = this.infoType
            param.PageIndex = page.num
            param.PageSize = page.size
            if (this.activity && this.activity.id) {
                param.ProductType = 3
                param.activityId = Number(this.activity.id)
            } else if (this.isLimit) {
                param.ProductType = 2
            }
            getWholeSaleListForSku(param).then((res) => {
                if (this.param.PageIndex == 1) {
                    this.datas = []
                }
                if (res.Data && res.Data.Data) {
                    this.mescroll.endBySize(res.Data.Data.length, res.RecordAmout)
                    this.datas = this.datas.concat(res.Data.Data)
                    this.changeArr(this.datas, true)
                } else {
                    let arr = []
                    this.mescroll.endBySize(0, 0)
                }
                this.$emit("getDataBack", "")
            })
        },
        // 整理数据
        changeArr(Arr, isNew) {
            const _this = this
            Arr.forEach((item) => {
                // 图片列表
                if (item.ImageUrl) {
                    const ImgUrlList = utilsOld.strToArr(item.ImageUrl)
                    item.logo = ImgUrlList[0]
                }
                if (item.ThumbnailUrl160) {
                    item.logo = item.ThumbnailUrl160
                }

                // 设置显示的动态值
                item.CurrentMaxPrice =
                    item.SaleMaxPrice % 1 === 0
                        ? item.SaleMaxPrice
                        : Number(item.SaleMaxPrice.toFixed(this.priceDecimalPlaces)) // 显示最高价格
                item.CurrentMinPrice =
                    item.SaleMinPrice % 1 === 0
                        ? item.SaleMinPrice
                        : Number(item.SaleMinPrice.toFixed(this.priceDecimalPlaces)) // 显示最低价格
                item.CurrentUnit = item.ProductUnitList[0].UnitName // 显示当前单位
                item.CurrentUnitId = item.ProductUnitList[0].UnitId // 显示当前单位ID
                item.CurrentStock = item.V_StockSum // 显示当前库存
                item.UnitActiveIndex = 0 // 单规格商品默认显示第一个加减框

                // 单位栏添加
                if (item.ProductUnitList.length > 0) {
                    item.ProductUnitList.forEach((ite) => {
                        ite.Title = ite.UnitName + (ite.Ratio !== 1 ? "=" + ite.Ratio + item.CurrentUnit : "")
                        this.initLoopArr(ite.ProductSkuAttrList)
                    })
                }

                // ProductSkuList数据修改
                item.ProductSkuList.forEach((ite) => {
                    ite.CurrentPrice =
                        ite.ProductUnitPriceList[0].Price % 1 === 0
                            ? ite.ProductUnitPriceList[0].Price
                            : Number(ite.ProductUnitPriceList[0].Price.toFixed(this.priceDecimalPlaces))
                    ite.CurrentUnit = ite.ProductUnitPriceList[0].UnitName // 显示当前单位
                    ite.CurrentUnitId = ite.ProductUnitPriceList[0].UnitId // 显示当前单位ID
                    ite.UnitActiveIndex = 0 // 单规格商品默认显示第一个加减框
                    ite.ProductUnitPriceList.forEach((it) => {
                        it.Amount = 0
                        it.Title = it.UnitName + (it.Ratio !== 1 ? "=" + it.Ratio + ite.CurrentUnit : "")
                    })
                })
                if (isNew) {
                    _this.productList.push(item)
                }
            })
        },
        // 遍历规格
        loopArr(arr, skuId = 0, unitId = 0, amount) {
            if (arr && arr.length > 0) {
                for (let i = 0; i < arr.length; i++) {
                    if (arr[i].ProductAttrList && arr[i].ProductAttrList.length > 0) {
                        this.loopArr(arr[i].ProductAttrList, skuId, unitId, amount)
                    } else {
                        if (
                            arr[i].SkuModel &&
                            arr[i].SkuModel.ProductUnitPrice &&
                            skuId === arr[i].SkuModel.ProductUnitPrice.SkuId &&
                            unitId === arr[i].SkuModel.ProductUnitPrice.UnitId
                        ) {
                            arr[i].SkuModel.ProductUnitPrice.Amount = amount
                        }
                    }
                }
            }
        },

        // 遍历规格，数量清零
        initLoopArr(arr) {
            if (arr && arr.length > 0) {
                for (let i = 0; i < arr.length; i++) {
                    if (arr[i].ProductAttrList && arr[i].ProductAttrList.length > 0) {
                        this.initLoopArr(arr[i].ProductAttrList)
                    } else {
                        if (arr[i].SkuModel && arr[i].SkuModel.ProductUnitPrice) {
                            arr[i].SkuModel.ProductUnitPrice.Amount = 0
                        }
                    }
                }
            }
        },
        refresh(requestData) {
            this.datas = []
            this.mescroll.resetUpScroll()
            this.$refs.proList.setUpNumTrue()
        },
        //点击空布局按钮的回调
        emptyClick() {
            uni.showToast({
                title: "点击了按钮,具体逻辑自行实现",
            })
        },
        sku(item) {
            this.$emit("sku", item)
        },
        upFavorite(e) {
            this.$emit("upFavorite", this.infoType)
        },
        isUpNum(e) {
            this.$emit("isUpNum", e)
        },
    },
}
</script>

<style lang="scss">
.mark {
    height: 20rpx;
}
</style>
